package com.ly.hotel.spainterface.pp.utils;

import android.text.TextUtils;
import android.util.Log;

import com.ly.hotel.common.MyApplication;
import com.ly.hotel.spainterface.pp.bean.PpJSPluginBean;
import com.ly.hotel.spainterface.pp.bean.PpXFPluginBean;
import com.ly.hotel.spainterface.pp.bean.PreItemIntroBean;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * Created by mo on 2018/5/25.
 */

public class PPXmlParseUtils {

    private static PPXmlParseUtils ppXmlParseUtils;
    private DocumentBuilder builder = null;
    private final String TAG = "PPXmlParseUtils";

    public static PPXmlParseUtils getInstance() {
        if (ppXmlParseUtils == null) {
            ppXmlParseUtils = new PPXmlParseUtils();
        }
        return ppXmlParseUtils;
    }

    private PPXmlParseUtils() {
        DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
        try {
            builder = domfac.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
    }

    // 解析技师配置
    public PpJSPluginBean parsePpJSPlugin(String pluginConfig, String pluginVersion) {
        PpJSPluginBean ppJSPlugin = null;
        try {
            Document doc = builder.parse(new ByteArrayInputStream(pluginConfig.getBytes("utf-8")));
            NodeList pluginVersionNode = doc.getElementsByTagName(pluginVersion);// 获得插件版本号下的节点
            for (int i = 0; i < pluginVersionNode.getLength(); i++) {
                ppJSPlugin = new PpJSPluginBean();
                NodeList jishiNode = ((Element) pluginVersionNode.item(i)).getElementsByTagName("jishi");// 获得jishi下的节点
                if (jishiNode.getLength() >= 1) {
                    // 解析ip
                    NodeList headNode = ((Element) jishiNode.item(0)).getElementsByTagName("ip");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String ip = headNode.item(0).getFirstChild().getNodeValue().trim();
                            ppJSPlugin.setIp(ip);
                        } catch (Exception e) {
                            Log.i(TAG, "ip配置出错！");
                        }
                    }

                    // 解析port
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("port");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String port = headNode.item(0).getFirstChild().getNodeValue().trim();
                            ppJSPlugin.setPort(port);
                        } catch (Exception e) {
                            Log.i(TAG, "port配置出错！");
                        }
                    }

                    // 解析showCall
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("showCall");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String showCall = headNode.item(0).getFirstChild().getNodeValue().trim();
                            boolean isShowCall = Boolean.parseBoolean(showCall);
                            ppJSPlugin.setShowCall(isShowCall);
                        } catch (Exception e) {
                            Log.i(TAG, "showCall配置出错！");
                        }
                    }

                    // 解析mode
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("mode");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String mode = headNode.item(0).getFirstChild().getNodeValue().trim();
                            ppJSPlugin.setMode(mode);
                        } catch (Exception e) {
                            Log.i(TAG, "mode配置出错！");
                        }
                    }

                    // 解析printPort
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("printPort");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String printPort = headNode.item(0).getFirstChild().getNodeValue().trim();
                            ppJSPlugin.setPrintPort(printPort);
                        } catch (Exception e) {
                            Log.i(TAG, "printPort配置出错！");
                        }
                    }

                    // 解析displayPrint
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("displayPrint");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String displayPrint = headNode.item(0).getFirstChild().getNodeValue().trim();
                            boolean isDisplayPrint = Boolean.parseBoolean(displayPrint);
                            ppJSPlugin.setDisplayPrint(isDisplayPrint);
                        } catch (Exception e) {
                            Log.i(TAG, "displayPrint配置出错！");
                        }
                    }
                }
            }
            if (TextUtils.isEmpty(ppJSPlugin.getIp()) || TextUtils.isEmpty(ppJSPlugin.getPort())) {
                try {
                    String[] ipAndPort = MyApplication.getHostIpAndPort().split(":");
                    ppJSPlugin.setIp(ipAndPort[0]);
                    ppJSPlugin.setPort(ipAndPort[1]);
                } catch (Exception e) {

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ppJSPlugin;
    }

    // 解析消费配置
    public PpXFPluginBean parsePpXFPlugin(String pluginConfig, String pluginVersion) {
        PpXFPluginBean ppXFPluginBean = null;
        try {
            Document doc = builder.parse(new ByteArrayInputStream(pluginConfig.getBytes("utf-8")));
            NodeList pluginVersionNode = doc.getElementsByTagName(pluginVersion);// 获得插件版本号下的节点
            for (int i = 0; i < pluginVersionNode.getLength(); i++) {
                ppXFPluginBean = new PpXFPluginBean();
                NodeList jishiNode = ((Element) pluginVersionNode.item(i)).getElementsByTagName("xiaofei");// 获得xiaofei下的节点
                if (jishiNode.getLength() >= 1) {
                    // 解析ip
                    NodeList headNode = ((Element) jishiNode.item(0)).getElementsByTagName("ip");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String ip = headNode.item(0).getFirstChild().getNodeValue().trim();
                            ppXFPluginBean.setIp(ip);
                        } catch (Exception e) {
                            Log.i(TAG, "ip配置出错！");
                        }
                    }

                    // 解析port
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("port");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String port = headNode.item(0).getFirstChild().getNodeValue().trim();
                            ppXFPluginBean.setPort(port);
                        } catch (Exception e) {
                            Log.i(TAG, "port配置出错！");
                        }
                    }

                    // 解析showCall
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("showCall");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String showCall = headNode.item(0).getFirstChild().getNodeValue().trim();
                            boolean isShowCall = Boolean.parseBoolean(showCall);
                            ppXFPluginBean.setShowCall(isShowCall);
                        } catch (Exception e) {
                            Log.i(TAG, "showCall配置出错！");
                        }
                    }

                    // 解析displayPrint
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("displayPrint");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String displayPrint = headNode.item(0).getFirstChild().getNodeValue().trim();
                            boolean isDisplayPrint = Boolean.parseBoolean(displayPrint);
                            ppXFPluginBean.setDisplayPrint(isDisplayPrint);
                        } catch (Exception e) {
                            Log.i(TAG, "displayPrint配置出错！");
                        }
                    }

                    // 解析mode
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("mode");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String mode = headNode.item(0).getFirstChild().getNodeValue().trim();
                            ppXFPluginBean.setMode(mode);
                        } catch (Exception e) {
                            Log.i(TAG, "mode配置出错！");
                        }
                    }

                    // 解析printPort
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("printPort");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String printPort = headNode.item(0).getFirstChild().getNodeValue().trim();
                            ppXFPluginBean.setPrintPort(printPort);
                        } catch (Exception e) {
                            Log.i(TAG, "printPort配置出错！");
                        }
                    }

                    // 解析printPort
                    headNode = ((Element) jishiNode.item(0)).getElementsByTagName("goodsCountLimit");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String goodsCountLimit = headNode.item(0).getFirstChild().getNodeValue().trim();
                            int goodsCount = Integer.parseInt(goodsCountLimit);
                            ppXFPluginBean.setGoodsCountLimit(goodsCount);
                        } catch (Exception e) {
                            Log.i(TAG, "goodsCountLimit配置出错！");
                        }
                    }
                }
            }
            if (TextUtils.isEmpty(ppXFPluginBean.getIp()) || TextUtils.isEmpty(ppXFPluginBean.getPort())) {
                try {
                    String[] ipAndPort = MyApplication.getHostIpAndPort().split(":");
                    ppXFPluginBean.setIp(ipAndPort[0]);
                    ppXFPluginBean.setPort(ipAndPort[1]);
                } catch (Exception e) {

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ppXFPluginBean;
    }

    // 解析项目介绍文档
    public List<PreItemIntroBean> parseItemIntroFile(String content) {
        List<PreItemIntroBean> preItemIntroBeans = new ArrayList<>();
        try {
            Document doc = builder.parse(new ByteArrayInputStream(content.getBytes("utf-8")));
            NodeList preNode = doc.getElementsByTagName("pre");
            for (int i = 0; i < preNode.getLength(); i++) {
                NodeList itemListNode = ((Element) preNode.item(i)).getElementsByTagName("itemList");
                for (int j = 0; j < itemListNode.getLength(); j++) {
                    PreItemIntroBean preItemIntroBean = new PreItemIntroBean();
                    NodeList headNode = ((Element) itemListNode.item(j)).getElementsByTagName("icode");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String icode = headNode.item(0).getFirstChild().getNodeValue().trim();
                            preItemIntroBean.setIcode(icode);
                            //Log.i(TAG,"icode=" + icode);
                        } catch (Exception e) {
                            //Log.i(TAG,"icode配置出错！");
                        }
                    }

                    headNode = ((Element) itemListNode.item(j)).getElementsByTagName("iname");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String iname = headNode.item(0).getFirstChild().getNodeValue().trim();
                            preItemIntroBean.setIname(iname);
                            //Log.i(TAG,"iname=" + iname);
                        } catch (Exception e) {
                            //Log.i(TAG,"iname配置出错！");
                        }
                    }

                    headNode = ((Element) itemListNode.item(j)).getElementsByTagName("intro");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String intro = headNode.item(0).getFirstChild().getNodeValue().trim();
                            preItemIntroBean.setIntro(intro);
                            //Log.i(TAG,"intro=" + intro);
                        } catch (Exception e) {
                            //Log.i(TAG,"intro配置出错！");
                        }
                    }

                    headNode = ((Element) itemListNode.item(j)).getElementsByTagName("taboo");
                    if (headNode.getLength() >= 1 && headNode.item(0) != null) {
                        try {
                            String taboo = headNode.item(0).getFirstChild().getNodeValue().trim();
                            preItemIntroBean.setTaboo(taboo);
                            //Log.i(TAG,"taboo=" + taboo);
                        } catch (Exception e) {
                            //Log.i(TAG,"taboo配置出错！");
                        }
                    }
                    preItemIntroBeans.add(preItemIntroBean);
                }
            }
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return preItemIntroBeans;
    }
}
